import TableUtils from '@/components/tables/cfg/table-utils.js'

import {
  edit_input,
  edit_switch,
  edit_input_number,
  edit_select_fixed,
  edit_select_align,
  edit_select_kind,
  edit_select_format,
  empty_formatter,
  switch_formatter,
  field_kinds_formatter,
  has_children_formatter,
  header_tooltip_icon
} from '@/components/tables/cfg/table-render.jsx';

export const useTableColumns = () => {
  return {
    getColumns: ({ fieldOptions }) => {
      return [
        {
          field: 'treeExpand',
          width: 130,
          align: 'left',
          treeNode: true,
          sortable: false,
          formatter: ({ cellValue, row, column }) => {
            if (TableUtils.isExistChildren(row)) {
              return '合并列';
            } else {
              return '数据列';
            }
          },
        },
        {
          field: 'dragLot',
          title: '',
          width: 40,
          align: 'center',
          sortable: false,
          className: 'drag-cell',
          slots: { default: 'drag_lot' },
        },
        {
          field: 'field',
          title: '字段名称',
          width: 200,
          align: 'left',
          sortable: false,
          formatter: params => empty_formatter(params),
          className: params => TableUtils.getNotChildrenClassName(params.row),
          slots: {
            default: params => {
              // if (fieldOptions && fieldOptions.length) {
              //   return edit_select_field(params, fieldOptions);
              // } else {
              //   return edit_input(params);
              // }
              return edit_input(params);
            },
          },
        },
        {
          field: 'title',
          title: '显示名称',
          width: 200,
          align: 'left',
          sortable: false,
          formatter: params => empty_formatter(params),
          slots: { default: params => edit_input(params, false) },
        },
        {
          field: 'width',
          title: '列宽',
          width: 80,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => has_children_formatter(params),
          slots: { default: params => edit_input_number(params) },
        },
        {
          field: 'align',
          title: '对齐方式',
          width: 100,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          slots: { default: params => edit_select_align(params) },
        },
        {
          field: 'visible',
          title: '是否显示',
          width: 80,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => switch_formatter(params),
          slots: { default: params => edit_switch(params) },
        },
        {
          field: 'sortable',
          title: '是否排序',
          width: 80,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => switch_formatter(params),
          slots: { default: params => edit_switch(params) },
        },
        {
          field: 'showOverflow',
          title: '显示省略号',
          width: 120,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => switch_formatter(params),
          slots: {
            header: params => header_tooltip_icon(params, '设置内容过长时是否显示省略号, 不开启内容将换行！'),
            default: params => edit_switch(params)
          },
        },
        {
          field: 'totaled',
          title: '是否合计',
          width: 100,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => has_children_formatter(params),
          slots: {
            header: params => header_tooltip_icon(params, '只对数据类型为数值类型的数据有效！'),
            default: params => edit_switch(params)
          },
        },
        {
          field: 'fixed',
          title: '固定位置',
          width: 120,
          align: 'center',
          sortable: false,
          className: ({ row, column }) => {
            if (!row.level || row.level !== 1) {
              return 'not-edit';
            } else {
              return '';
            }
          },
          slots: { default: params => edit_select_fixed(params) },
          formatter: params => empty_formatter(params),
        },
        // {
        //   field: 'kind',
        //   title: '数据类型',
        //   width: 130,
        //   align: 'center',
        //   sortable: false,
        //   formatter: params => field_kinds_formatter(params),
        //   className: params => TableUtils.getNotChildrenClassName(params.row),
        //   slots: { default: params => edit_select_kind(params) },
        // },
        {
          field: 'formatRule',
          title: '格式化规则',
          width: 210,
          align: 'left',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          slots: {
            default: params => edit_select_format(params),
          },
        },
        {
          field: 'formatFunc',
          title: '格式化函数',
          width: 108,
          align: 'center',
          sortable: false,
          className: params => {
            let notEdit = TableUtils.getNotChildrenClassName(params.row)
            return `cell-full ${notEdit}`
          },
          formatter: params => has_children_formatter(params),
          slots: { default: 'edit_format_func' },
        },
        {
          field: 'linkUrl',
          title: '超链接信息',
          width: 230,
          align: 'left',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => has_children_formatter(params),
          slots: { default: 'edit_input_link' },
        },
        {
          field: 'renderFunc',
          title: '渲染函数',
          width: 80,
          align: 'center',
          sortable: false,
          className: params => TableUtils.getNotChildrenClassName(params.row),
          formatter: params => has_children_formatter(params),
          slots: { default: 'edit_render_func' },
        },
      ];
    },
  };
};

